<?php

class Default_GroupController extends Zend_Controller_Action
{

    public $groupname = null;
    public $pagenumber = null;
    public $index = null;
    public $dataURI;
    public $Session;
    public $ExecStart;
    public function init()
    {
        $this->ExecStart = time();
        $this->_redirector = $this->_helper->getHelper('Redirector');        
        Zend_Session::start();
        $this->Session = new Zend_Session_Namespace ('Params');
    }

    public function pictureAction()
    {
        echo "Deprecated action!"; 
        // deprecated - moved to RequestController
    }

    public function watchAction()
    {
        echo "Deprecated action!"; 
        // deprecated - moved to RequestController
    } 

    public function listAction()
    {
        $request    = $this->getRequest(); 
        $this->groupname  = $request->getParam('group'); 
        $this->pagenumber = $request->getParam('page'); 
        $this->pagefilter = $this->Session->Type;  
        $this->pageparam  = $this->Session->Search; 
        $this->savedparam = $this->Session->Saved; 
         
        $startIndex  = ($this->pagenumber - 1) * 8;
        $listObject  = $this->view->ArticleList($this->groupname);

        $bookMarker  = new Default_Model_BookmarkManager;
        $userProfile = new Default_Model_UserProfile;
        $newsServer  = new Default_Model_NewsServer;

        $groupObject = $newsServer->groups[$this->groupname];
        $groupObject->sync(); 
  
        $this->view->pagenumber  = $this->pagenumber;

        $this->view->headerText  = $this->view->partial( "relate.phtml", 
                                    array("groups"      => array(),#$groupObject->RelatedGroups,
                                           "searches"    => $groupObject->SavedSearches,
                                           "saved"       => $this->savedparam,
                                           "filter"      => $this->pagefilter,
                                           "param"       => $this->pageparam,
                                           "group"       => $this->groupname,
                                           "persisted"   => $this->Session->Persisted,
                                           "liststyle"   => $this->Session->ListStyle,
                                           "subscribed"  => $userProfile->IsSubscribed($this->groupname) ) );
        $obj = array_slice ($listObject->articlesets, $startIndex, 8);
        foreach ($obj as $o) $o->CheckPreview($this->groupname);
        $this->view->Persisted   = $this->Session->Persisted;
        $this->view->liststyle   = $this->Session->ListStyle;
        $this->view->pageparam   = $this->pageparam;
        $this->view->groupname   = $this->groupname;
        $this->view->manager     = $bookMarker;
        $this->view->newsgroup   = $groupObject;
        $this->view->filter      = $this->pagefilter;
        $this->view->subscribed  = $userProfile->IsSubscribed($this->groupname) ? 'Unsubscribe' : 'Subscribe';
        $this->view->articlesets = $obj;
        $this->view->articlesize = sizeof($listObject->articlesets); 
        $this->view->exectime    = time() - $this->ExecStart;  
    }

    public function loadAction()
    { 
        echo "Deprecated action!"; 
        // deprecated - moved to RequestController
    }

    public function filterAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $filterset  = $request->getParam('unset'); 
        $modereset  = $request->getParam('reset'); 
        $fullsearch = $request->getParam('full'); 
        $layout     = $request->getParam('layout'); 
        $origin     = $request->getHeader('referer'); 

        if (isset ($filterset)) {
            $this->Session->$filterset = NULL;
                $this->_redirector
                     ->gotoUrl("/request/pagegroup/groupname/{$groupname}/pagenumber/1"); 
                return;
        }

        if (isset ($layout)) { 
            $this->_helper->layout->setLayout($layout);
        }

        $form       = new Application_Form_GroupFilter; 
        $server     = new Default_Model_NewsServer(true);

        $form->Create($origin);
        $this->view->form      = $form;
        $this->view->info      = $server->GetGroupInfo ($groupname);
        $this->view->groupname = $groupname;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $values = $form->getValues(); 
                $fields = $form->getElements(); 
                $full   = $values['full']; 
                $goto   = $values['goto']; 
                $find   = $values['find']; 
                $type   = $values['filter']; 
                $target = $values['target']; 

                if (isset($full) && $full=='1') {  
                    $this->_helper->getHelper("FlashMessenger")->addMessage($goto);
                    if (strlen($target) > 0)
                        $this->_helper->getHelper("FlashMessenger")->addMessage($target);
                    $this->_redirector
                         ->gotoUrl("/request/fullsearch/groupname/{$groupname}/param/{$find}"); 
                    return;
                }

                $this->Session->Search = $find;
                $this->Session->Type   = $type;
                $this->_redirector
                     ->gotoUrl("/request/pagegroup/groupname/{$groupname}/pagenumber/1"); 
                return;
            }
        } 
    }

    public function updateAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $server     = new Default_Model_NewsServer;
        $group      = $server->groups[$groupname];
        $group->sync();
        $form       = new Application_Form_GroupUpdate;
        $form->create($group->MinID);
 
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $values = $form->getValues(); 
                $start  = $values['start'];
                $count  = $values['count']; 
                $target = $_POST['target']; 
                if (strlen($target) > 0) {  
                    $this->_helper->getHelper("FlashMessenger")->addMessage($target);
                }
                $this->_redirector
                     ->gotoUrl("/request/join/groupname/{$groupname}/count/{$count}/start/{$start}");
                return;
            }
        } 
        $this->view->form      = $form;
        $this->view->groups    = $group->RelatedGroups;
        $this->view->groupname = $groupname;
    }

    public function indexAction()
    { 
        echo "Deprecated action!"; 
        // moved to RequestController
    }

    public function bookmarkAction()
    {
        echo "Deprecated action!"; 
        // moved to RequestController
    }

    public function subscribeAction()
    {
        echo "Deprecated action!"; 
        // moved to RequestController
    }


}




function redirect($url) { echo "<script>location.href='{$url}'</script>"; }




